

import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import java.awt.event.KeyEvent;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import javax.swing.JTextArea;

public class Demo7 extends JFrame {

	private JPanel contentPane;
	
	private File file = null;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Demo7 frame = new Demo7();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Demo7() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		JTextArea textArea = new JTextArea();
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		
		JMenu menuFile = new JMenu("文件");
		
		JMenuItem menuItemOpen = new JMenuItem("打开");
		menuItemOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_MASK));
		
		menuItemOpen.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				try {
					JFileChooser c = new JFileChooser();
					int r = c.showOpenDialog(null);
					if( r == JFileChooser.APPROVE_OPTION ){
						file = c.getSelectedFile(); 
						if( ! file.exists() ){
							file.createNewFile();
						}
						System.out.println(file.getAbsolutePath());
						//读io
					}
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		
		JMenuItem menuItemSave = new JMenuItem("保存");
		menuItemSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK));
		menuItemSave.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					if( file == null || ! file.exists() ){
						JFileChooser c = new JFileChooser();
						int r = c.showSaveDialog(null);
						if( r == JFileChooser.APPROVE_OPTION ){
							file = c.getSelectedFile();
							if( ! file.exists() ){
								file.createNewFile();
							}
						} else {
							return;
						}
					}
					//io保存
					FileWriter fw = new FileWriter(file);
					fw.write(textArea.getText());
					fw.flush();
					fw.close();
				} catch (Exception e2) {
					// TODO: handle exception
				}
			}
		});
		
		
		menuFile.add(menuItemOpen);
		menuFile.add(menuItemSave);
		menuBar.add(menuFile);
		
		
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		
		textArea.setBounds(10, 27, 371, 149);
		contentPane.add(textArea);
	}
}
